package com.zenitera.bigdata.sink;

import com.zenitera.bigdata.bean.WaterSensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;

public class Flink04_Sink_Jdbc {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);


        ArrayList<WaterSensor> waterSensors = new ArrayList<>();
        waterSensors.add(new WaterSensor("sensor_1", 1607527992000L, 20));
        waterSensors.add(new WaterSensor("sensor_1", 1607527994000L, 50));
        waterSensors.add(new WaterSensor("sensor_1", 1607527996000L, 50));
        waterSensors.add(new WaterSensor("sensor_2", 1607527993000L, 10));
        waterSensors.add(new WaterSensor("sensor_2", 1607527995000L, 30));


        DataStreamSource<WaterSensor> stream = env.fromCollection(waterSensors);

        SingleOutputStreamOperator<WaterSensor> result = stream
                .keyBy(WaterSensor::getId)
                .sum("vc");

        result.addSink(new MySqlSink());


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class MySqlSink extends RichSinkFunction<WaterSensor> {

        private Connection connection;

        @Override
        public void open(Configuration parameters) throws Exception {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://hdt-dmcp-ops05:3306/test?useSSL=false", "root", "123456");
        }

        @Override
        public void close() throws Exception {
            if (connection != null) {
                connection.close();
            }
        }

        @Override
        public void invoke(WaterSensor value,
                           Context context) throws Exception {

            String sql = "replace into sensor(id, ts, vc)values(?,?,?)";

            PreparedStatement ps = connection.prepareStatement(sql);

            ps.setString(1, value.getId());
            ps.setLong(2, value.getTs());
            ps.setInt(3, value.getVc());

            ps.execute();

            ps.close();

        }
    }
}

/*
MariaDB [test]> CREATE TABLE `sensor` (
    ->   `id` varchar(20) NOT NULL,
    ->   `ts` bigint(20) NOT NULL,
    ->   `vc` int(11) NOT NULL,
    ->   PRIMARY KEY (`id`,`ts`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.063 sec)

MariaDB [test]> select * from sensor;
+----------+---------------+-----+
| id       | ts            | vc  |
+----------+---------------+-----+
| sensor_1 | 1607527992000 | 120 |
| sensor_2 | 1607527993000 |  40 |
+----------+---------------+-----+
2 rows in set (0.000 sec)

 */